package com.kiln.juxingqs.dialog

import android.graphics.drawable.ColorDrawable
import androidx.databinding.ObservableField
import androidx.fragment.app.FragmentActivity
import client.global.commlib.toast.toast
import com.kiln.juxingqs.R
import com.kiln.juxingqs.databinding.DialogSelectdaterangeBinding
import com.loper7.date_time_picker.DateTimeConfig
import com.tzstore.oo.dialog.BottomBindingDialog
import java.text.SimpleDateFormat
import java.util.*

class SelectDateRangeDialog(context: FragmentActivity) :
    BottomBindingDialog<DialogSelectdaterangeBinding>(context) {

    val titleTxt = ObservableField<String>()

    var isLongDate = ObservableField<Boolean>(false)

    var startDate: String? = ""
    var endDate: String? = ""


    override fun getV(): Int {
        return R.layout.dialog_selectdaterange
    }

    val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd")

    var selectCli: ((String, String) -> Unit)? = null

    override fun postBinding() {
        binding.event = this


        binding.startPicker.setGlobal(DateTimeConfig.GLOBAL_LOCAL)
        binding.startPicker.setDisplayType(
            intArrayOf(DateTimeConfig.YEAR, DateTimeConfig.MONTH, DateTimeConfig.DAY)
        )
        binding.endPicker.setGlobal(DateTimeConfig.GLOBAL_LOCAL)
        binding.endPicker.setDisplayType(
            intArrayOf(DateTimeConfig.YEAR, DateTimeConfig.MONTH, DateTimeConfig.DAY)
        )

        if (!startDate.isNullOrEmpty()) {
            binding.startPicker.setDefaultMillisecond(
                simpleDateFormat.parse(startDate).time
            )
        }
        if (!endDate.isNullOrEmpty())
            binding.endPicker.setDefaultMillisecond(
                simpleDateFormat.parse(endDate).time
            )
    }

    fun ok() {
        val s = binding.startPicker.getMillisecond()
        val e = binding.endPicker.getMillisecond()
        if(!isLongDate.get()!!){
            if (e < s) {
                toast("开始日期不能大于结束日期")
                return
            }
        }
        selectCli?.invoke(
            simpleDateFormat.format(Date(s)).toString(),
            simpleDateFormat.format(Date(e)).toString()
        )
        cancel()
    }

    fun reset() {
        selectCli?.invoke(
            "",
            ""
        )
        cancel()
    }

}